﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DbSharp.Utils.Db
{
    public class SqlTextHelper
    {


        /// <summary>
        /// Delete character of char1 in the ending of str string
        /// e.g. trim ; char in the ending of SQL
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="char1"></param>
        /// <returns></returns>
        public static string RightTrimCharacter(string str, char char1)
        {
            str = str.Replace('\n', ' ');
            str = str.Replace('\t', ' ');
            str = str.Trim();
            int index;
            index = str.LastIndexOf(char1);
            while (index == str.Length - 1)
            {
                str = str.Substring(0, index);
                str = str.Trim();
                index = str.LastIndexOf(char1);
            }
            return str;
        }


        /// <summary>
        /// Delete character of char1 in the leading of str string
        /// e.g. delete the leading @ char in parameter name
        /// </summary>
        /// <param name="str"></param>
        /// <param name="char1"></param>
        /// <returns></returns>
        public static string LeftTrimCharacter(string str, char char1)
        {
            str = str.Replace('\n', ' ');
            str = str.Replace('\t', ' ');
            str = str.Trim();
            int index;
            index = str.IndexOf(char1);
            while (index == 0)
            {
                str = str.Substring(index + 1, str.Length);
                str = str.Trim();
                index = str.IndexOf(char1);
            }
            return str;
        }


        /// <summary>
        /// Make sure the str to begin with char1
        /// e.g. add the leading @ char in parameter name
        /// </summary>
        /// <param name="str"></param>
        /// <param name="char1"></param>
        /// <returns></returns>
        public static string BeginWithCharacter(string str, char char1)
        {
            str = str.Replace('\n', ' ');
            str = str.Replace('\t', ' ');
            str = str.Trim();
            int index;
            index = str.IndexOf(char1);
            if (index == -1)
            {
                str = char1 + str;
            }
            return str;
        }


        /// <summary>
        /// make sure the str to end with char1
        /// e.g. add ; char in the ending of SQL
        /// </summary>
        /// <param name="str"></param>
        /// <param name="char1"></param>
        /// <returns></returns>
        public static string EndWithCharacter(string str, char char1)
        {
            str = str.Replace('\n', ' ');
            str = str.Replace('\t', ' ');
            str = str.Trim();
            int index;
            index = str.LastIndexOf(char1);
            if (index != str.Length - 1)
            {
                str = str+char1;
            }
            return str;
        }


    }
}
